package com.shop.service.impl;

import com.shop.constant.Constants;
import com.shop.dao.ShoppingCartDao;
import com.shop.dao.impl.ShoppingCartDaoImpl;
import com.shop.entity.LookCartVO;
import com.shop.exception.*;
import com.shop.factory.ObjectFactory;
import com.shop.service.ShoppingCartService;
import com.shop.transaction.TransactionManager;

import java.util.List;

public class ShoppingCartServiceImpl implements ShoppingCartService {

    private ShoppingCartDao shoppingCartDao = (ShoppingCartDaoImpl) ObjectFactory.getInstance(Constants.SHOPPING_CART_DAO_KEY);

    //用户查看购物车
    @Override
    public List<LookCartVO> lookCart(int u_id) throws DAOException {

        List<LookCartVO> list = null;
        list = shoppingCartDao.lookCart(u_id);
        if(list == null) {
            throw new DAOException();
        }
        for (LookCartVO l : list) {
            String g_four = shoppingCartDao.getFourById(l.getGd_id());
            int img_id = shoppingCartDao.getImgId(l.getGd_id());
            if(g_four == null) {
                throw new DAOException();
            }
            l.setG_fourname(g_four);
            l.setImg_id(img_id);
        }

        return list;
    }

    @Override
    public int deleteGoodsById(int sc_id, int sd_id) throws DAOException, DeleteGoodsFromCartFailException {
        TransactionManager tm = new TransactionManager();
        int result = 0;
        try {
            //开启事务
            //tm.openTransaction();
            //下面是业务处理代码
            result = shoppingCartDao.deleteGoodsById(sc_id,sd_id);
            if(result == 0) {
                throw new DeleteGoodsFromCartFailException("商品删除失败");
            }
            //业务处理代码写完后需要提交
            //tm.commit();
        } catch (DAOException e) {
            //如果报错，这里需要回滚
            //tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
        return result;
    }

    @Override
    public int changeNum(int sc_id, int sd_id, int sd_num) throws DAOException, ChangeNumFromCartFailException {
        TransactionManager tm = new TransactionManager();
        int result = 0;
        try {
            //开启事务
            //tm.openTransaction();
            //下面是业务处理代码
            result = shoppingCartDao.changeNum(sc_id,sd_id,sd_num);
            if(result == 0) {
                throw new ChangeNumFromCartFailException("商品数量修改失败");
            }
            //业务处理代码写完后需要提交
            //tm.commit();
        } catch (DAOException e) {
            //如果报错，这里需要回滚
            //tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
        return result;
    }

    @Override
    public int changeState(int sc_id, int sd_id, int sd_state) throws DAOException, ChangeStateFromCartFailException {
        TransactionManager tm = new TransactionManager();
        int result = 0;
        double total = 0;
        try {
            //开启事务
            //tm.openTransaction();
            //下面是业务处理代码
            result = shoppingCartDao.changeState(sc_id,sd_id,sd_state);
            if(result == 0) {
                throw new ChangeStateFromCartFailException("商品数量修改失败");
            }
            //查看购物车总金额
            total = shoppingCartDao.changeTotal(sc_id,sd_state);
            //修改购物车总金额
            result = shoppingCartDao.changeCartTotal(sc_id,total);
            //业务处理代码写完后需要提交
            //tm.commit();
        } catch (DAOException e) {
            //如果报错，这里需要回滚
            //tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
        return result;
    }

    @Override
    public int clearCart(int sc_id) throws CartClearFailException, DAOException {
        TransactionManager tm = new TransactionManager();
        int result = 0;
        try {
            //开启事务
            tm.openTransaction();
            //下面是业务处理代码
            result = shoppingCartDao.clearCart(sc_id);
            if(result == 0) {
                throw new CartClearFailException("购物车清空失败");
            }
            //业务处理代码写完后需要提交
            tm.commit();
        } catch (DAOException e) {
            //如果报错，这里需要回滚
            tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
        return result;
    }

    @Override
    public int addCart(int sc_id, int gd_id, int sd_num, int sd_state) throws DAOException {
        TransactionManager tm = new TransactionManager();
        int result = 0;
        try {
            //开启事务
            //tm.openTransaction();
            //下面是业务处理代码
            result = shoppingCartDao.addCart(sc_id,gd_id,sd_num,sd_state);
            //业务处理代码写完后需要提交
            //tm.commit();
        } catch (DAOException e) {
            //如果报错，这里需要回滚
            //tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
        return result;
    }

    @Override
    public String getGoodsImg(String gd_id) throws DAOException {
        String path = null;
        path = shoppingCartDao.getGoodsImg(gd_id);
        if(path == null) {
            throw new DAOException();
        }
        return path;
    }

    @Override
    public double getTotal(int u_id) throws DAOException {
        int result = 0;

        int sc_id = shoppingCartDao.getCartId(u_id);
        double total = shoppingCartDao.changeTotal(sc_id,1);

        return total;
    }

    @Override
    public int changeStateSingle(int sc_id, int sd_id, int sd_state) throws DAOException, ChangeStateFromCartFailException {
        TransactionManager tm = new TransactionManager();
        int result = 0;
        double total = 0;
        try {
            //开启事务
            //tm.openTransaction();
            //下面是业务处理代码
            result = shoppingCartDao.changeStateSingle(sc_id,sd_id,sd_state);
            if(result == 0) {
                throw new ChangeStateFromCartFailException("商品数量修改失败");
            }
            //查看购物车总金额
            total = shoppingCartDao.changeTotal(sc_id,sd_state);
            //修改购物车总金额
            result = shoppingCartDao.changeCartTotal(sc_id,total);
            //业务处理代码写完后需要提交
            //tm.commit();
        } catch (DAOException e) {
            //如果报错，这里需要回滚
            //tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
        return result;
    }
}
